<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Cart;
use App\Services\Redis;
class CartController extends Controller
{
    /**
     * 购物车接口开发
     */
    public function Cart(Request $request){
        //获取用户id
        $userId=$request->get('user_id');
        //判断是否为空
        if(empty($userId) && !is_numeric($userId)){
            return ['code'=>400,'msg'=>'参数错误','data'=>null];
        }

        //redis取出购物车数据
       $redisRest= json_decode(Redis::getValueFromHashKey('cartInfo','user_id'),true);

        //判断数据是否为空
        if(empty($redisRest)) {

            //查询购物车数据
            $cartInfo = json_encode(Cart::where('user_id', '=', $userId)->get());
            //将数据存入到redis
            $redisRest = Redis::setValueToHashKey('cartInfo', 'user_id', $cartInfo);

        }
            return  ['code'=>200,'msg'=>'查询成功','data'=>$redisRest];

    }
}
